package logic

import (
	"context"
	"errors"
	"fmt"
	"math/rand"
	"strconv"
	"user-srv/common"
	"user-srv/model/redis"

	"user-srv/internal/svc"
	"user-srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *user.SendSmsRequest) (*user.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	intn := rand.Intn(100000)
	sms, err := common.SendSms(in.Mobile, strconv.Itoa(intn))
	if err != nil {
		return nil, errors.New("短信发送失败")
	}
	fmt.Println(sms)
	err = redis.AddSendSms(in.Mobile, strconv.Itoa(intn))
	if err != nil {
		return nil, errors.New("redis保存失败")
	}
	return &user.SendSmsResponse{Success: true}, nil
}
